<?php
/*
YHM (Your Hosting Manager) - Copyright 2010 All Rights Reserved. - YHM Group
Released under the Simplified BSD Licence.

Website: http://yhm.co.uk
Licence: http://yhm.co.uk/about/#licence

file: admin/subdomains.php
author: Kieran D. (Polarbear541)
*/

//Init Includes, Sessions and Page Info
require_once("../global.php");
session_start();
checklogin_admin();
$username = $_SESSION['user_name'];
$page = "settings"
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>YHM - Admin CP - Subdomains</title>
<meta http-equiv="Content-Language" content="English" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />
</head>
<body>

<?php include("./header.php"); ?>

<?php
	//Assign Variables
	$action = $_GET['action'];
	$id = mysql_real_escape_string($_GET['id']);
	
	if ($action == "create") //If creating show form
	{
		echo "<h2>Creating Subdomain</h2>";
	
		echo "<form action='./subdomains.php?action=do_create' method='POST'>
Subdomain: <input type='text' name='domain' /><br />
Server/s: <select name='server'><option value='0'>All Servers</option>";

		$getserv = mysql_query("SELECT * FROM ".TABLE_PREFIX."servers ORDER BY id DESC");
		
		while ($servers = mysql_fetch_array($getserv))
		{
			echo "<option value='{$servers['id']}'>{$servers['name']} ({$servers['hostname']})</option>";
		}

		echo "</select><br />
Active: <input type='checkbox' name='active' value='active' checked='yes' /><br /><br />
<input type='submit' value='Submit'></form><br />";
	}
	
	elseif ($action == "do_create") //If doing the creation run queries after standard checks
	{
		$domain = trim(htmlspecialchars(mysql_real_escape_string($_POST['domain'])));
		$active = trim(htmlspecialchars(mysql_real_escape_string($_POST['active'])));
		
		if ($active == "active")
		{
			$status = "1";
		}
		else
		{
			$status = "0";
		}
		
		if (empty($domain))
		{
			echo "<h2>There was a problem!</h2>";
			echo "Please fill in all the fields!<br />";
            echo "<a href='./subdomains.php?action=create'>Click here to go back</a><br />";
		}
		
		else
		{
			$createsub = mysql_query("INSERT INTO ".TABLE_PREFIX."subdomains VALUES (NULL, '$domain','$server','$status')");
			
			if (!$createsub) //Error Handling
			{
				echo "<h2>There was a problem!</h2>";
				echo mysql_errno() . ": Query Failed" . mysql_error();
				echo "<a href='./subdomains.php?action=create'>Click here to go back</a><br />";
			}
			else
			{
				echo "<h2>Subdomain Created Successfully</h2>";
				echo "<a href='./subdomains.php'>Click here to go back if not redirected</a><br />";
				redirect("./subdomains.php", 2);
			}
		}
	}
	
	elseif ($action == "edit") //If editing show form
	{
		//Run Queries to get id info
		$query = mysql_query("SELECT * FROM ".TABLE_PREFIX."subdomains WHERE id='$id'");
		$row = mysql_fetch_array($query);
		$count = mysql_num_rows($query);
		
		if ($row['status'] == "1")
		{
			$checked = "checked='$active'";
		}
		
		if ($count == 0)
		{
			echo "<h2>There was a Problem!</h2>";
			echo "The subdomain you requested does not exist.<br /><br /><a href='./subdomains.php>Click here to go back</a>";
		}
		
		elseif (!empty($id)) //Check if ID exists
		{
			echo "<h2>Editing Subdomain - {$row['domain']}</h2>";
	
			echo "<form action='./subdomains.php?action=do_edit&id=$id' method='POST'>
Subdomain: <input type='text' name='domain' value='{$row['domain']}' /><br />
Server/s: <select name='server'>";

			if ($row['sid'] == "0")
			{
				echo "<option selected='selected' value='0'>All Servers</option>";
			}
			else
			{
				echo "<option value='0'>All Servers</option>";
			}

			if ($row['status'] == "1")
			{
				$checked = "checked='$active'";
			}
			
			$getserv = mysql_query("SELECT * FROM ".TABLE_PREFIX."servers ORDER BY id DESC");
			
			while ($servers = mysql_fetch_array($getserv))
			{
				if ($row['sid'] == $servers['id'])
				{
					echo "<option selected='selected' value='{$servers['id']}'>{$servers['name']} ({$servers['hostname']})</option>";	
				}
				
				else
				{
					echo "<option value='{$servers['id']}'>{$servers['name']} ({$servers['hostname']})</option>";
				}
			}

			echo "</select><br />
Active: <input type='checkbox' name='active' value='active' $checked /><br /><br />
<input type='submit' value='Submit'></form><br />";
		}
		
		else
		{
			echo "<h2>There was a Problem!</h2>";
			echo "Please specify a subdomain id to edit!<br /><a href='./subdomains.php'>Click here to go back</a>";
		}
	}
	
	elseif ($action == "do_edit") //If doing the edit run queries after standard checks
	{
		$domain = trim(htmlspecialchars(mysql_real_escape_string(($_POST['domain']))));
		$server = trim(htmlspecialchars(mysql_real_escape_string(($_POST['server']))));
		$active = trim(htmlspecialchars(mysql_real_escape_string(($_POST['active']))));
	
		if ($active == "active")
		{
			$status = "1";
		}
		else
		{
			$status = "0";
		}
		
		if (empty($domain))
		{
			echo "<h2>There was a Problem!</h2>";
			echo "Please fill in all the fields!<br />";
            echo "<a href='./subdomains.php?action=edit&id=$id'>Click here to go back</a><br />";
		}
		
		elseif (!empty($id)) //Check if ID exists
		{
			$editsub = mysql_query("UPDATE ".TABLE_PREFIX."subdomains SET domain='$domain', sid='$server', status='$status' WHERE id=$id;");
			
			if (!$editsub) //Error Handling
			{
				echo "<h2>There was a Problem!</h2>";
				echo "Error " . mysql_errno() . ": Query Failed - " . mysql_error() . "<br />";
				echo "<a href='./subdomains.php?action=edit&id=$id'>Click here to go back</a><br />";
			}
			else
			{
				echo "<h2>Subdomain Edited Successfully</h2>";
				echo "<a href='./subdomains.php'>Click here to go back if not redirected</a><br />";
				redirect("./subdomains.php", 2);
			}
		}
		
		else
		{
			echo "<h2>There was a Problem!</h2>";
			echo "Please specify a subdomain id to edit!<br /><a href='./subdomains.php'>Click here to go back</a>";
		}
	}
	
	elseif ($action == "delete") //If Deleting Run Queries
	{
		if (empty($id)) //Check if ID exists
		{
			echo "<h2>There was a Problem!</h2>";
			echo "Please specify a subdomain id to delete!<br /><a href='./subdomains.php'>Click here to go back</a>";
		}
		
		else
		{
			$delete = mysql_query("DELETE from ".TABLE_PREFIX."subdomains WHERE id='$id'");
			
			if (!$delete) //Error Handling
			{
				echo "<h2>There was a Problem!</h2>";
				echo "Error " . mysql_errno() . ": Query Failed" . mysql_error();
				echo "<a href='./subdomains.php'>Click here to go back</a><br />";
			}
			else
			{
				echo "<h2>Subdomain Deleted Successfully</h2>";
				echo "<a href='./subdomains.php'>Click here to go back if not redirected</a><br />";
				redirect("./subdomains.php", 2);
			}
		}
	}
	
	else //Else list all
	{
		echo "<h2>Subdomains - <a href='./subdomains.php?action=create'>Create</a></h2>";
		echo "<table width='100%'>
<thead align='left'>
<th>Subdomain</th>
<th>Server</th>
<th>Status</th>
<th>Options</th>
</thead>
<tbody>";
	
		$getsubs = mysql_query("SELECT * FROM ".TABLE_PREFIX."subdomains ORDER by id DESC") or die("Error: Query Failed" . mysql_error());
	
		while ($sublist = mysql_fetch_array($getsubs))
		{
			//Convert Status ID's to Languages - 1:Active 0:Disabled
			if ($sublist['status'] == "1")
			{
				$status = "Active";
			}
			elseif ($sublist['status'] == "0")
			{
				$status = "Disabled";
			}
			
			//Get Server name from Server ID
			$getserv = mysql_query("SELECT * FROM ".TABLE_PREFIX."servers WHERE id='{$sublist['sid']}'");
			$serv = mysql_fetch_array($getserv);
			$server = $serv['name'] . " ({$serv['hostname']})";
			
			//If status is set to all display "All Servers"
			if ($sublist['sid'] == 0)
			{
				$server = "All Servers";
			}
		
			echo "<tr>";
			echo "<td><a href='./subdomains.php?action=edit&id={$sublist['id']}'>{$sublist['domain']}</a></td>";
			echo "<td>$server</td>";
			echo "<td>$status</td>";
			echo "<td><a href='./subdomains.php?action=edit&id={$sublist['id']}'><img src='../images/edit.png' title='Edit' alt='Edit' /></a> <a href='./subdomains.php?action=delete&id={$sublist['id']}'><img src='../images/delete.png' title='Delete' alt='Delete' /></a></td>";
			echo "</tr>";
		}
		
		echo "</tbody></table>";
	}
	?>

<?php include ("../footer.php"); ?>
</div>
</body>
</html>